package game.johan.engine;

/*
Copyright (C) 2013 Johan Ceuppens

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/


import java.nio.ByteBuffer;

import android.graphics.Bitmap;
import android.graphics.Canvas;

/*
 * This should become a bucket hashtable
 */

public class BitmapHashTable
{
	byte _bytearray[][];
	int _capacity1 = 0;
	int _capacity2 = 0;
	
	int markoffset = 0;
	int mark = 0;
	
	EngineBase _base;
	Canvas _canvas;
	
	public BitmapHashTable(int capacity1, int capacity2, Canvas canvas, EngineBase base) {
		
		_canvas = canvas;
		_base = base;
		
		_capacity1 = capacity1;
		_capacity2 = capacity2;
		_bytearray = new byte[capacity1][capacity2];
		
	}
	
	public void add(Canvas canvas, EngineBase base, Bitmap b) {
		Surface surf = new Surface(b);
		//drawOnCanvasBaseBitmap(Bitmap b)
		int m = b.getWidth()*b.getHeight()+markoffset++;
		if (m > mark)
			mark = m; 
		if (m > _capacity1) {
			_capacity1 = m+1;
			int i = 0;
			byte ba[][] = new byte[_capacity1][_capacity2];
			while (i < _bytearray.length)
				ba[i] = _bytearray[i];
			_bytearray = ba;
		}
		if (surf.toByteArray().length > _capacity2)
			_bytearray[m] = new byte[surf.toByteArray().length+1];
		_bytearray[m] = surf.toByteArray();
	}
	
	public void add(Bitmap b) {
		Surface surf = new Surface(b);
		//drawOnCanvasBaseBitmap(Bitmap b)
		_bytearray[b.getWidth()*b.getHeight() % markoffset++] = surf.toByteArray();
	}
	
/*	public byte[] get(Bitmap b) {
		return (_bytearray[b.getWidth()*b.getHeight() % markoffset]);
	}
*/
	
	public void drawOn(Surface surf) {
		int i = 0;
		for ( ; i < mark; i++) {
			byte[] bytes = _bytearray[i];
			if (bytes != null) {
				int bytesize = bytes.length;
				ByteBuffer bb = ByteBuffer.allocate(bytesize); 
				bb.put(bytes, 0, bytesize);
				surf.drawOnByteBuffer(bb);
			}
		}
	}
}